package com.chrelyonly.util.wx;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * 微信模板发送
 *
 * @author chrelyonly
 */
public class WxSendUtil {

    /**
     * 模板ID
     */
    static final String TEMPLATE = "x78EF_WtI9RbiG12DeEWZohfSCmxexGIlT9gqhtrBCI";
    /**
     * 点击链接
     */
    static final String URL = "";
    /**
     * 发送用户
     */
    static final List<String> OPENID = new ArrayList<>();

    /**
     * 封装邮件参数
     *
     */
    public static List<String> wxMpTemplateMessage() throws WxErrorException {
        OPENID.clear();
//        封装模板信息
        List<WxMpTemplateData> wxMpTemplateDataList = new ArrayList<>();
        wxMpTemplateDataList.add(new WxMpTemplateData("first", "新消息", "#ffffff"));
        wxMpTemplateDataList.add(new WxMpTemplateData("keyword1", "啦啦啦!", "#473E4F"));
        wxMpTemplateDataList.add(new WxMpTemplateData("keyword2", "你好", "#499C54"));
        wxMpTemplateDataList.add(new WxMpTemplateData("keyword3", "1234", "#73CDC5"));
        wxMpTemplateDataList.add(new WxMpTemplateData("keyword4", "1234", "#C75450"));
        wxMpTemplateDataList.add(new WxMpTemplateData("keyword5", "1234", "#A9687C"));
        wxMpTemplateDataList.add(new WxMpTemplateData("remark", "1234", "#4CE979"));
//        存下发送是否成功的等信息
        List<String> restSend = new ArrayList<>();
        OPENID.add("o2pF86Zv7jDexeqEHLKZnCX2xq5w");
        for (String s : OPENID) {
            restSend.add(sendMessage(wxMpTemplateDataList, TEMPLATE, URL, s));
        }
        return restSend;
    }

    /**
     * 发送邮件
     */
    public static String sendMessage(List<WxMpTemplateData> wxMpTemplateDataList, String template, String url, String openid) throws WxErrorException {
        final WxMpService wxAuth2Service = WxInIt.wxMpService();
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().templateId(template).url(url).toUser(openid).build();
        for (WxMpTemplateData wxMpTemplateData : wxMpTemplateDataList) {
            templateMessage.addData(wxMpTemplateData);
        }
        return wxAuth2Service.getTemplateMsgService().sendTemplateMsg(templateMessage);
    }
}
